package com.mfnote.view.hand;

import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.mfnote.adapter.HandAdapter;
import com.mfnote.db.dao.HandDao;
import com.mfnote.util.FinalData;
import com.mfnote.view.R;
import com.mfnote.view.R.anim;
import com.mfnote.view.R.id;
import com.mfnote.view.R.layout;
import com.mfnote.view.main.MainActivity;
import com.mfnote.view.other.MyApplication;

/**
 * 手绘图形界面
 * @author DC
 *
 */
public class HandActivity extends Activity implements OnClickListener,
		OnItemClickListener, OnItemLongClickListener {
	//新建手绘
	private Button hand_titlebar_btn_add = null;
	//手绘列表
	private ListView hand_lv = null;
	private AlertDialog alertDlgIsDel; //长按记录弹出删除提示框
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case FinalData.HANDACTIVITY_DELETE_SUCCESS:
				hand_lv.setAdapter(new HandAdapter(getApplicationContext()));
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hand);
		MyApplication.getInstance().addActivity(this);
		hand_titlebar_btn_add = (Button) findViewById(R.id.hand_titlebar_btn_add);
		hand_lv = (ListView) findViewById(R.id.hand_lv);

		hand_titlebar_btn_add.setOnClickListener(this);
		hand_lv.setAdapter(new HandAdapter(this));
		// 单击Item
		hand_lv.setOnItemClickListener(this);
		// 长按事件
		hand_lv.setOnItemLongClickListener(this);
	}
    
	/**Button的事件监听**/
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.hand_titlebar_btn_add:
			//Intent intent = new Intent(this, NewHandActivity.class);
			Intent intent = new Intent(this, HandWritingActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(R.anim.push_right_in,
					R.anim.push_left_out);
			break;
		case R.id.hand_btn_query:
			// TODO 查询
			break;
		case R.id.hand_btn_batchdelete:
			// TODO 批量删除
			break;

		}
	}

	/**单击Item事件**/
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = null;
		if(position==0){
			//默认跳转到HandWritingActivity
			intent = new Intent(this, HandWritingActivity.class);
		}else{
			//带数据跳转
			HashMap<String, Object> map = (HashMap<String, Object>) parent.getItemAtPosition(position);
			Log.d("flage", map.get("flag").toString());
			if("0".equals(map.get("flag").toString())){
				//跳转到手绘界面
				intent = new Intent(this, NewHandActivity.class);
			}else if("1".equals(map.get("flag").toString())){
				intent = new Intent(this, HandWritingActivity.class);
			}
			intent.putExtra("map", map);
		}
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.push_right_in,
				R.anim.push_left_out);
	}

	/**长按Item事件**/
	@Override
	public boolean onItemLongClick(final AdapterView<?> parent, View view,
			final int position, long id) {
		if (position == 0) {
			return false;
		} else {
			// 弹出对话框
			LayoutInflater inflater=LayoutInflater.from(this);
			View viewDialog=inflater.inflate(R.layout.dialog_isdelete, null);
			alertDlgIsDel= new AlertDialog.Builder(this).create();
			alertDlgIsDel.show();
			Display display=this.getWindowManager().getDefaultDisplay();
			int width=display.getWidth();
			int height=display.getHeight();
			LayoutParams layoutParams=new LayoutParams(width*90/100,LayoutParams.WRAP_CONTENT);	
			alertDlgIsDel.setContentView(viewDialog, layoutParams);
			Button btn_delete_sure=(Button) viewDialog.findViewById(R.id.btn_delete_sure);
			Button btn_delete_cancel=(Button) viewDialog.findViewById(R.id.btn_delete_cancel);
			btn_delete_sure.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//删除数据
					HashMap<String,Object> map = (HashMap<String, Object>) parent.getItemAtPosition(position);
					long state = new HandDao(getApplicationContext()).deleteHand(map);
					if(state>0){
						Toast.makeText(HandActivity.this, "删除成功",Toast.LENGTH_SHORT).show();
						//删除成功,更新主界面
						Message msg = new Message();
						msg.what = FinalData.HANDACTIVITY_DELETE_SUCCESS;
						handler.sendMessage(msg);
					}else{
						Toast.makeText(getApplicationContext(), "删除失败", Toast.LENGTH_SHORT).show();
					}
					alertDlgIsDel.dismiss();
				}
			});
			btn_delete_cancel.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					alertDlgIsDel.dismiss();
				}
			});
		}
		return true;
	}

	/**监听返回处理**/
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			this.finish();
			overridePendingTransition(R.anim.alpha_in,R.anim.scale_suoxiao_out
					);
		}
		return true;
	}

}
